package cpt202.project.controller;
import cpt202.project.entity.Book;
import cpt202.project.entity.Record;
import cpt202.project.entity.User;
import cpt202.project.service.*;
import cpt202.project.service.exception.*;
import cpt202.project.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private InitialUserInfo initialUserInfo;

    @RequestMapping("/reg")
    public JsonResult<Void> reg(User user){
        JsonResult<Void> result = new JsonResult<>();
        try {
            userService.Register(user);
            result.setState(200);//200代表用户注册成功
            result.setMessage("用户注册成功");
    }
        catch(ServiceException e){
            if (e instanceof EmailDuplicatedException){
                result.setState(3000);
                result.setMessage("邮箱已被注册");

            }
            else if (e instanceof InsertException){
                result.setState(4000);
                result.setMessage("发生未知错误");
            }
        }
        return result;
    }

    @RequestMapping("/resetpsw")
    public JsonResult<Void> resetPwd(String email,String password){
        JsonResult<Void> result = new JsonResult<>();
        try{
            userService.resetpsw(email,password);
           result.setState(200);
           result.setMessage("重置密码成功");
        }
        catch (UserNotExistException e ){
            result.setState(6000);
            result.setMessage("邮箱不存在");
        }
        catch (updateException e){
            result.setState(5000);
            result.setMessage("更改密码时发生未知错误");
        }
        return result;
    }

    @RequestMapping("/login")
    //通过参数的形式将session注入
    public JsonResult<User> login(String email, String password, HttpSession httpSession){
        JsonResult<User> result = new JsonResult<>();
        try{
            User ret = userService.login(email,password);
            if(ret!=null){
                //将session对象注入键值对然后传回去
                httpSession.setAttribute("email",ret.getEmail());
                result.setState(200);
                result.setMessage("登录成功");
                result.setData(ret);
            }
        }

        catch(UserNotExistException e){
            result.setState(6000);
            result.setMessage("邮箱不存在");
        }
        catch (PwdIncorrectException e){
            result.setState(7000);
            result.setMessage("输入的密码有误");
        }
        return result;
    }


    //用于在首页初始化时返回用户默认账本的records
    @RequestMapping("/information")
    public JsonResult< Record[] > retUserRec(HttpServletRequest request){
        JsonResult< Record[] > result = new JsonResult<>();
        Object email = request.getSession().getAttribute("email");
        Record[] records=initialUserInfo.retRec((String) email);
        result.setData(records);
        result.setState(200);
        return result;
    }


    //用于页面初始化时返回用户所有的账本id和名称
    @RequestMapping("/Books")
    public JsonResult<Book[]> retBookid(HttpServletRequest request){
        JsonResult< Book[] > result = new JsonResult<>();
        Object email = request.getSession().getAttribute("email");
        Book[] books = initialUserInfo.retAllBid((String)email);
        result.setState(200);
        result.setData(books);
        return result;
    }
}
